package handler

import (
	"encoding/json"
	"fmt"
	"server/initialize"
	. "server/model"

	"github.com/gomodule/redigo/redis"
)

func SetJson(list []GameData) error {
	//conn := initialize.RedisConn.Get()
	//defer conn.Close()
	//marshal, err := json.Marshal(list)
	//if err != nil {
	//	return err
	//}
	//
	//_, err = conn.Do("Set", "gamedata", string(marshal))
	//if err != nil {
	//	fmt.Println("redis set err = ", err)
	//	return err
	//}
	return nil
}

// GetJson 通过redis获取[]GameData
func GetJson() (*[]GameData, error) {
	var list *[]GameData = new([]GameData)
	conn := initialize.RedisConn.Get()
	defer conn.Close()
	result, err := redis.Bytes(conn.Do("Get", "gamedata"))
	if err != nil {
		fmt.Println("redis get err = ", err)
		return nil, err
	}

	err = json.Unmarshal(result, &list)
	if err != nil {
		fmt.Println("redis get json unmarshal err = ", err)
		return nil, err
	}
	return list, nil

}
